<?php

m2import('base/ApplicationContext.php');
m2import('phpinchains/psp/ViewDispatcher.php');

/**
 * Default Action
 *
 */
class DocumentAction extends phpinchains_WebAction
{

    function create (& $env, & $mapping, & $af)
    {
        $this->wizzard($env, $af, 
            array(
                array("document/docform.psp", "wzform", "_addDoc"),
                array("document/navnameform.psp", "wzform", "_addNavnames")
            ),
            "newDoc"
        );
    }
    
    function _addDoc (&$env, &$actionform, $sessionprefix, $step) {
        $ctx = & base_ApplicationContext::getInstance();
        $ldba   = $ctx->accessFactory->getInstanceOf('CMSLang');
        $check  = $ldba->getLanguages($langtable,true);
        $dba    = $ctx->accessFactory->getInstanceOf('CMS', $langtable);
        $usr    = & $ctx->getUsr();
        $permission = & $usr->getDocumentPermission($af->get('parent_id'));
        if ($permission->add() && $permission->filesystem()) 
        {
            $userdata = $usr->exportUserData();
            $doc_id = $dba->insertDoc($env->getSession('s_currentSite'),$af->get('name'),$af->get('parent_id'),$af->get('type'),$af->get('nav_visible'),$af->get('sitemap_visible'),$af->get('headertpl'),$af->get('contenttpl'),$af->get('footertpl'),$langtable,$af->get('weighting'),$userdata['data']['id']);
            $GLOBALS['doc_id'] = $doc_id;
            if ($doc_id == -1)
            {
                $af->set('emsg', "'An dieser Stelle existiert bereits eine Node mit diesem Namen!'");
            } 
            elseif ($doc_id)
            {
                if ($af->get('type') != 'folder')
                {
                    $doc  = $dba->getDoc($doc_id);
                    $templatecode = array();
                    if (file_exists($ctx->cfgPath('template').'cms/header/'.$doc['headertpl'])) 
                    {
                        $templatecode = file($ctx->cfgPath('template').'cms/header/'.$doc['headertpl']);
                    }
                    if (file_exists($ctx->cfgPath('template').'cms/content/'.$doc['contenttpl'])) 
                    {
                        $templatecode = array_merge($templatecode,file($ctx->cfgPath('template').'cms/content/'.$doc['contenttpl']));
                    }
                    if (file_exists($ctx->cfgPath('template').'cms/footer/'.$doc['footertpl'])) 
                    {
                        $templatecode = array_merge($templatecode,file($ctx->cfgPath('template').'cms/footer/'.$doc['footertpl']));
                    }
                    $weighting = ($af->get('weighting'))?$af->get('weighting'):0;
                    $tpl = new templateAnalyser($dba,$templatecode,$ctx->cfgPath('template').'cms/',$doc_id,$weighting);
                    $GLOBALS['tpl'] = & $tpl;
                    if (!$tpl) die('Template nicht gefunden!');
                    // document-struktur (is:tag-struktur)
                    $struct = new elements($tpl->getStructure());
                    $GLOBALS['struct'] = & $struct;
                    walk_add(0,0);
                    $dba->setPublishType($doc_id,$docPublishType);
                }
            }
        }
    }

    function _addNavnames (&$env, &$actionform, $sessionprefix, $step) {
        print "add navnames";
    }

}

?>